//获取应用实例
const app = getApp();
let eventChannel;
Page({
	data: {
		src:'',
		width: 214,//宽度
		height: 284,//高度
		max_width: 428,
		max_height: 568,
		disable_rotate:true,//是否禁用旋转
		limit_move: true,//是否限制移动
		disable_width: true,
		disable_height: true,
		index: null,
		showImage: false,
		isLoading: false
	},
	onLoad: function (options) {
		eventChannel = this.getOpenerEventChannel();
		if (options.type == 1) { //头像尺寸
			this.setData({
				width: 200,//宽度
				height: 200,//高度
				max_width: 400,
				max_height: 400,
			})
		}
		if (options.type == 2) { //主页头图
			this.setData({
				width: 300,//宽度
				height: 200,//高度
				max_width: 750,
				max_height: 500,
			})
		}
		this.setData({
			index: options.index,
			type: options.type,
			showImage: true
		})
		this.cropper = this.selectComponent("#image-cropper");
		this.upload();//上传图片
	},
	upload(){
		let that = this;
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				const tempFilePaths = res.tempFilePaths[0];
				//重置图片角度、缩放、位置
				that.cropper.imgReset();
				that.setData({ src: tempFilePaths });
			}
		})
	},
	submit(){
		if (this.data.isLoading) return false;
		this.setData({
			isLoading: true
		})
		wx.showLoading({
			title: '上传中',
		})
		this.cropper.getImg((obj)=> {
			if (obj.result) {
				eventChannel.emit('getUrl', { url: obj.result, index: this.data.index, type: this.data.type });
				wx.navigateBack({ delta: -1 })
			} else {
				wx.showModal({ title: '错误提示2', content: '上传图片失败', showCancel: false })
			}
			wx.hideLoading()
			this.setData({
				isLoading: false
			})
		});
	}
})
